package presenter;

import java.awt.BorderLayout;
import java.awt.Component;
import java.io.File;
import java.net.URL;
import javax.media.Manager;
import javax.media.Player;
import javax.swing.JFrame;
import javax.swing.JPanel;

/**
 * @author Al Lamarre
 */
public class VideoPanel extends JPanel {
    
    Component videoControls;
    Component video;
    Player player;
    File f;
    
    public VideoPanel(int x, int y) {
        setSize(x,y);
    }
    
    public void loadVideo(String s) {
        try{
            File file=new File(s);
            System.out.println(file.listFiles());
            loadVideo(file.toURL());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    
    public void loadVideo(URL url) {
        Manager.setHint(Manager.LIGHTWEIGHT_RENDERER, true);
        f=new File(url.getFile());
        try {
            player=Manager.createRealizedPlayer(url);
            video = player.getVisualComponent();
            
            //setSize(video.getSize());
            setLayout(new BorderLayout());
            if(video!=null)
                add(video,BorderLayout.CENTER);
            videoControls=player.getControlPanelComponent();
            //add(videoControls);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    
    public void start() {
        player.start();
    }
    
    public Component getVideo() {
        return video;
    }
    
    public Player getPlayer() {
        return player;
    }
    
    public String toString() {
        String name=f.toString();
        int beginExt=name.lastIndexOf(".");
        if(beginExt>0)
            return name.substring(0,beginExt);
        
        return name;
    }
    
    public Component getVideoControls() {
        return videoControls;
    }
    
    public static void main (String[] args) {
        JFrame jf=new JFrame();
        
        VideoPanel vp=new VideoPanel(800,600);
        jf.pack();
        vp.loadVideo("Imaginationland2.avi");
        vp.start();
        jf.setContentPane(vp);
        jf.setVisible(true);
        jf.setDefaultCloseOperation(jf.DISPOSE_ON_CLOSE);
        
    }
    
}
